//
class ParticleSystemEx extends particle.GravityParticleSystem {
	offx = 0;

	offy = 0;

	// constructor(texture: egret.Texture, config: any) {
	// 	super(texture, config);
	// 	this.setBlend(1); // 0 normal 1 add 2 erase!
	// }

	setOffset(x: number, y: number): void {
		this.offx = x;
		this.offy = y;
	}

	setBlend(blend: number) {
		this['particleBlendMode'] = blend;
	}

	// 但是因为nativeEngine做了优化，修改此处无用！
	// public advanceParticle(particle: particle.Particle, dt: number): void {
	// 	// 粒子运动变化
	// 	super.advanceParticle(particle, dt);
	// }
}

async function createParticle(
	tex: string,
	json: string,
	time: number,
	blendmode = 0,
): Promise<ParticleSystemEx> {
	const texture = await loadTextureByUrl(tex);
	const config = await loadJsonByUrl(json);

	// 因为feather没有用blendMode，而是使用blendFactorSource,blendFactorDestination
	if (blendmode != null) {
		config.blendMode = blendmode;
	}
	// config.blendMode = 1;

	const system = new ParticleSystemEx(texture, config);
	system.start(time);
	return system;
}

async function createParticleName(
	name: string,
	time: number,
	blendmode = 0,
): Promise<ParticleSystemEx> {
	return createParticle(`particle/${name}.png`, `particle/${name}.json`, time, blendmode);
}

async function autoAddParticleName(
	parent: egret.DisplayObjectContainer,
	name: string,
	time: number,
	blendmode = 0,
): Promise<ParticleSystemEx> {
	const p = await createParticleName(name, time, blendmode);
	parent.addChild(p);
	p.addEventListener(
		egret.Event.COMPLETE,
		() => {
			p.stop(true);
			safeRemoveFromFather(p);
		},
		null,
	);
	return p;
}
